package com.eyet.comeon.net;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;

import com.eyet.comeon.ui.UI;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

/**
 * 网络工具类
 * 功能：
 * 1、网络检测
 * 2、网络传输
 * @author WHL
 *
 */
public class Net {
	
	private static final int REQUEST_TIMEOUT = 6000;//设置请求超时10秒钟  
	private static final int SO_TIMEOUT = 6000;  //设置等待数据超时时间10秒钟  
	
	//private static String SERVER_IP = "192.168.0.134";
	private static String SERVER_IP = "www.mygxu.net";
	private static String SERVER_PRO = "http://";
	private static String SERVER_APP_URL = "/comeon/";
	
	public static String getSERVER_IP() {
		return SERVER_IP;
	}

	public static void setSERVER_IP(String sERVER_IP) {
		SERVER_IP = sERVER_IP;
	}

	
	/**
	 * 检查网络状态
	 * 
	 * @param context
	 * @return 
	 * true 真确建立连接
	 * false 没有建立任何连接
	 */
	public static boolean testConnectivity(Context context){
    	ConnectivityManager connManager = (ConnectivityManager) context.
    		getSystemService(Context.CONNECTIVITY_SERVICE);//获取系统的连接服务
    	NetworkInfo activeNetInfo = connManager.getActiveNetworkInfo();//获取网络的连接情况
    	if(activeNetInfo == null){
    		UI.createDialog(context, "当前网络设置错误，请查看手机网络情况，稍后重新登录。", 2);
    		return false;
    	}
    	if(activeNetInfo.getType()==ConnectivityManager.TYPE_WIFI){ //1
    		//判断WIFI网
    		Log.i("通知", "WIFI网络已连接");
    		return true;
    	}else if(activeNetInfo.getType()==ConnectivityManager.TYPE_MOBILE) { //0
    		//判断手机网
    		Log.i("通知", "手机网络已连接");
    		return true;
    	}else{
    		Log.i("通知", "没有连接");
    		UI.createDialog(context, "当前网络设置错误，请查看手机网络情况，稍后重新登录。", 2);
    		return false;
    	}
    }
	
	/**
	 * 网络发送方法
	 * 使用post发送信息，结果返回json
	 * 
	 * @param params
	 * @return
	 * null  处理不成功，发生错误等返回null
	 * String 返回字符串结果
	 */
	public static String sendHttpRequest(String req , List<NameValuePair> params){
		
		String SERVER_URL = SERVER_PRO + SERVER_IP + SERVER_APP_URL + req;
		try {
			String strResult = null;
			HttpPost httpRequest = new HttpPost(SERVER_URL);
			if(params != null){
				HttpEntity httpentity = new UrlEncodedFormEntity(params, "utf-8");
				httpRequest.setEntity(httpentity);
			}
			//HttpClient httpclient = new DefaultHttpClient();
			HttpClient httpclient = getHttpClient();
			HttpResponse httpResponse = httpclient.execute(httpRequest);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				
				strResult = EntityUtils.toString(httpResponse.getEntity() , "utf-8");
			
			}else{
				Log.e("返回代码错误" , "return status code :" + httpResponse.getStatusLine().getStatusCode());
				return null;
			}
			return strResult.trim();
			
		} catch (UnsupportedEncodingException e) {
			Log.e("编码错误" , "httpentity code error");
			return null;
		} catch (ParseException e) {
			Log.e("错误" , "ParseException");
			return null;
		} catch (IOException e) {
			Log.e("错误" , "IOException");
			return null;
		}
		
	}
	
	/**
	 * 获取设置有超时的httpClient
	 */
	private static HttpClient getHttpClient(){  
	    BasicHttpParams httpParams = new BasicHttpParams();  
	    HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);  
	    HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);  
	    HttpClient client = new DefaultHttpClient(httpParams);  
	    return client;  
	}  
	
}
